﻿using System;
using System.Collections.Generic;
using Dune.Business.Domain.Forum;
using Dune.Business.Domain.Users;
using Dune.Business.Managers;
using Edge.Common;
using Edge.Common.ComponentModel;
using Edge.Common.DataUtils;
using Edge.Common.Services;
using Edge.Common.Utils;
using Dune.DAL.Managers.Users;
using Edge.DAL.Common;
using Edge.DAL.Managers;
using NHibernate;
using NHibernate.Expression;

namespace Dune.DAL.Managers
{
    class ThemeManager : AbstractManager<Theme, int, User>,  IThemeManager
    {
        public ICollection<Theme> GetCategories(int? id)
        {
            IList<Theme> ret = null;
            using (IDalOperation op = CreateLoadOperation())
            {
                ICriteria criteria = CreateCriteria();
                if (id != null && id != 0)
                    criteria.Add(Expression.Eq("ParentTheme.Id", id));
                else
                    criteria.Add(Expression.IsNull("ParentTheme"));
                ret = criteria.List<Theme>();
                op.Commit();
            }
            return ret;
        }
        public ICollection<Theme> Find(FetchOptions fetchOptions, ThemeFilter filter)
        {
            try
            {
                ICriteria criteria = CreateCriteria(fetchOptions);
                AddRestrictions(criteria, filter);
                return criteria.List<Theme>();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Count(ThemeFilter filter)
        {
            ICriteria criteria = CreateCriteria();
            AddRestrictions(criteria, filter);
            criteria.SetProjection(Projections.RowCount());
            return (int)criteria.List()[0];
        }

        public Theme GetLastUpdatedTheme(Theme theme)
        {
            Theme ret = null;
            using (IDalOperation op = CreateLoadOperation())
            {
                if (theme != null && theme.Children != null && theme.Children.Count > 0)
                {
                    ICriteria criteria = CreateCriteria();
                    criteria.AddOrder(Order.Desc("CreateDate"));
                    ret = criteria.List<Theme>()[0];
                    op.Commit();
                }
                else
                    return null;
            }
            return ret;
        }

        private void AddRestrictions(ICriteria criteria, ThemeFilter filter)
        {
            if (!StringUtils.IsBlankOrNull(filter.GetText()))
            {
                criteria.Add(Expression.Disjunction()
                                 .Add(Expression.Like("Description", "%" + filter.GetText().Trim() + "%"))
                                 .Add(Expression.Like("Name", "%" + filter.GetText().Trim() + "%")));
            }

            if (filter.CreatedBy != null)
            {
                criteria.Add(Expression.Eq("CreatedBy", filter.CreatedBy));
            }

            if (filter.Parent != null)
            {
                criteria.Add(Expression.Eq("ParentTheme", filter.Parent));
            }
        }



        public ICollection<NavigationItem> ConvertToNavigation(ICollection<Theme> themes, Theme currentTheme)
        {
            ICollection<NavigationItem> ret = new List<NavigationItem>();
            foreach (Theme theme in themes)
            {
                if (currentTheme != null && currentTheme.Equals(theme))
                    ret.Add(theme.ConvertToNavigation(true));
                else
                    ret.Add(theme.ConvertToNavigation());
            }
            return ret;
        }


    }
}